export const socket = {
    init () {
        if (typeof WebSocket === "undefined") {
            alert("您的浏览器不支持socket");
        } else {
            console.log(error,'23333')
            // 实例化socket
            window.socket = new WebSocket(process.env.VUE_APP_IM_SOCKET_SERVER +"?token=" +sessionStorage.getItem("token"));
            // 监听socket连接
            window.socket.onopen = open;
            // 监听socket错误信息
            window.socket.onerror = error;
            // 监听socket消息
            window.socket.onmessage = getMessage;
        }
    },
        // 连接成功
         open :()=> {
            try {
                console.log("socket连接成功", this.socket.readyState);
                //  setTimeout(()=>{
                //     this.send("begin");
                //    this.send(JSON.stringify(this.params));
                //  },3000)
            } catch (e) {
                console.log("1111111111");
            }
        },
        //  错误
        error:()=> {
                console.log("1111111111");

        },
        //   收到消息
         getMessage:()=> {
            try {
                console.log("收到消息", msg.data);
            } catch (e) {
                console.log("发送失败");
            }
        },
        //   发送消息
         send:(params)=> {
            this.socket.send(params);
            try {
                console.log("发送成功", params);
            } catch (e) {
                console.log("发送失败");
            }
        },
        //   关闭
         close:()=> {
            try {
                console.log("socket已经关闭", close);
            } catch (e) {
                console.log("发送失败");
            }
        }
}

 
